<?php

namespace service;

use app\common\controller\Api;
use Qiniu\Storage\BucketManager;
use think\Request;

class Video extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->key = 'vDoTmhcOTzX3yJ8yZVdOV74SrS';
    }
    public function get($url)
    {
        $api_url = 'https://www.345api.cn/api/jiexi/get';
        $get_post_data = array(
            //接口参数，一行一个，可按照用户控制台->开发工具 的参数填写，或者直接复制开发工具下面的测试代码。
            'key' => $this->key,
            'url' => $url,
        );
        $sk = '267f34f75f389877eddfdae2f9368f20';
        return self::send($api_url,$get_post_data,'GET', true, $sk);
    }
    public function query($ip){
        $api_url = 'https://www.345api.cn/api/ipv2/query';
        $get_post_data = array(
            //接口参数，一行一个，可按照用户控制台->开发工具 的参数填写，或者直接复制开发工具下面的测试代码。
            'key' => $this->key,
            'ip' => $ip,
        );
        $sk = '267f34f75f389877eddfdae2f9368f20';
        return self::send($api_url,$get_post_data,'GET', true, $sk);
    }
    public static function send($API_URL, $get_post_data, $type, $ifsign, $sk)
    {
        $get_post_data = http_build_query($get_post_data);
        if ($ifsign) {
            $sign = md5($get_post_data . $sk);
            $res = self::send_curl($API_URL, $type, $get_post_data, $sign);
        } else {
            $res = self::send_curl($API_URL, $type, $get_post_data, null);
        }
        return $res;
    }
    //封装好的CURL请求函数,支持POST|GET
    public static function send_curl($API_URL, $type, $get_post_data, $sign)
    {
        $ch = curl_init();
        if ($type == 'POST') {
            curl_setopt($ch, CURLOPT_URL, $API_URL);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $get_post_data);
        } elseif ($type == 'GET') {
            curl_setopt($ch, CURLOPT_URL, $API_URL . '?' . $get_post_data);
        }
        if ($sign) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, ['sign:' . $sign]);
        }
        curl_setopt($ch, CURLOPT_REFERER, $API_URL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $resdata = curl_exec($ch);
        curl_close($ch);
        return $resdata;
    }
    public function sendQi($url){
        $config = get_addon_config('qiniu');
        // 构建鉴权对象
        $auth = new \Qiniu\Auth($config['accessKey'], $config['secretKey']);
        $bucketManager = new BucketManager($auth);
        $token = $auth->uploadToken($config['bucket']);
        $fileName = 'uploads/'.date('Ymd')."/".md5($url).".mp4";
        // 远程上传
        list($ret, $err) = $bucketManager->fetch($url, $config['bucket'], $fileName);

        if ($err !== null) {
            // 上传失败
            return ['code' => 0, 'msg' => '远程上传失败', 'error' => $err];
        } else {
            // 上传成功
            return ['code' => 1, 'msg' => 'success','data'=>"/".$ret['key']];
        }
    }
    public function sendImg($url,$unlink = null){
        $extension = pathinfo($url, PATHINFO_EXTENSION);
        $config = get_addon_config('qiniu');
        // 构建鉴权对象
        $auth = new \Qiniu\Auth($config['accessKey'], $config['secretKey']);
        $bucketManager = new BucketManager($auth);
        $token = $auth->uploadToken($config['bucket']);
        if (empty($unlink))
            $fileName = 'uploads/'.date('Ymd')."/".md5($url).".$extension";
        else
            $fileName = $unlink;
        // 远程上传
        list($ret, $err) = $bucketManager->fetch($url, $config['bucket'], $fileName);

        if ($err !== null) {
            // 上传失败
            return ['code' => 0, 'msg' => '远程上传失败', 'error' => $err];
        } else {
            // 上传成功
            return ['code' => 1, 'msg' => 'success','data'=>"/".$ret['key']];
        }
    }

}